//
//  WeightNextViewController.m
//  Light
//
//  Created by 骁唐 on 15/8/28.
//  Copyright (c) 2015年 骁唐. All rights reserved.
//

#import "WeightNextViewController.h"
#import "UserConfig.h"
#import "AFNetworking.h"
#import "NetworkTool.h"
#import "constantDefinition.h"
#import "LoginViewController.h"
@interface WeightNextViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *weightPickerView;
@property (strong,nonatomic) NSMutableArray *kgArray;
//当前体重
@property (copy,nonatomic) NSString *weight;
@property (weak, nonatomic) IBOutlet UIImageView *backGround;

@end

@implementation WeightNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.weight = @"60";
    self.kgArray = [[NSMutableArray alloc]init];
    for  (int i = 20; i< 100; i++) {
        [self.kgArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    [self.weightPickerView selectRow:40 inComponent:0 animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//影藏pickView的分割线
- (void)viewDidLayoutSubviews {
    ((UIView *)[_weightPickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
    ((UIView *)[_weightPickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    //返回有几列 ，注意
    return 1;
    
}

//返回指定列的行数
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return  [self.kgArray count];
}

//以下是代理部分，可以自定义视图

//返回指定列，行的高度，就是自定义行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
   // NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    
    return  60;
}
//返回指定列的宽度
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
  //  NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    return 80;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
   // NSLog(@"调用%s ,%d",__FUNCTION__,__LINE__);
    
    
    //思想就是：先创建一个View以指定列的宽度，和所在列行的高度，为尺寸
    //再建立一个label,在这个view上显示字体，字体颜色，字体大小，然后，把这个label添加到view中
    //返回view，作为指定列的每行的视图
    
    //取得指定列的宽度
    CGFloat width=[self pickerView:pickerView widthForComponent:component];
    
    //取得指定列，行的高度
    CGFloat height=[self pickerView:pickerView rowHeightForComponent:component];
    
    //定义一个视图
    UIView *myView=[[UIView alloc] init];
    //指定视图frame
    myView.frame=CGRectMake(0, 0, width, height);
    myView.backgroundColor = [UIColor clearColor];
    
    UILabel *labelOnComponent=[[UILabel alloc] init];
    
    labelOnComponent.frame=myView.frame;
//    labelOnComponent.tag=200;
    
    //在label上显示改字体
    labelOnComponent.text= [self.kgArray objectAtIndex:row];
    labelOnComponent.textColor = [UIColor whiteColor];
    labelOnComponent.textAlignment = UIBaselineAdjustmentAlignCenters;
    [labelOnComponent setNumberOfLines:0];
    labelOnComponent.font = [UIFont fontWithName:@"Helvetica" size:35];
    labelOnComponent.adjustsFontSizeToFitWidth = YES;
    
    [myView addSubview:labelOnComponent];
    return myView;
}
- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
{
    if (0 == component) {
        self.weight = self.kgArray[row];
    }
}

#pragma mark -
#pragma mark 下一步，提交注册登录
- (IBAction)nextClick {
//    [self performSegueWithIdentifier:@"weightToConnect" sender:nil];
    UserConfig *userConfig = [UserConfig sharingUserConfig];
    [userConfig setCurrentWeight:self.weight];
  
    //传的参数
    if ([userConfig getToken].length == 0) {
        [userConfig setToken:@""];
    }
    
    NSDictionary *dic= @{@"Token":[userConfig getToken],@"humanHeight":[userConfig getCurrentHeight],@"grade":@"1",@"age":[userConfig getCurrentAge],@"medicalhistory":@"",@"region":@"",@"gender":[userConfig getCurrentUserGender],@"name":[userConfig getCurrentNickName],@"birthday":[userConfig getCurrentBirthday],@"weight":self.weight,@"bloodType":@""};
    __weak WeightNextViewController *weakSelf = self;
    AFHTTPRequestOperationManager *httpManager = [AFHTTPRequestOperationManager manager];
    httpManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    httpManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //地址
    [httpManager POST:SET_INFO parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *retDic = [NetworkTool toNSDictionary:responseObject];
        if ([retDic[@"a"] intValue] == 1)
        {
              UserConfig *config = [UserConfig sharingUserConfig];
              [config setHaveLogin:@"login"];//设置登录标记
              [self performSegueWithIdentifier:@"weightToConnect" sender:nil];
            
        }else if ([retDic[@"a"] intValue] == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                            message:retDic[@"b"]
                                                           delegate:weakSelf
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
        }
        else if([retDic[@"a"] intValue] == -1)
        {
            if (TJJiOS8)
            {//iOS8 以上使用
                UIAlertController  * Alertview =  [UIAlertController alertControllerWithTitle:@"您的登录过期,请您重新登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
                [self presentViewController:Alertview animated:YES completion:nil];
                
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"重新登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    UserConfig *config = [UserConfig sharingUserConfig];
                    [config setHaveLogin:@"lalala"];
                    [config setToken:@"123"];
                    [config setUserHeadData:nil];
                    
                    UIStoryboard *LoginSt = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
                    LoginViewController *loginVc = [LoginSt instantiateViewControllerWithIdentifier:@"loginViewId"];
                    loginVc.quit = YES;
                    [self.navigationController pushViewController:loginVc animated:YES];
                }];
                [Alertview addAction:action];
            }
            else
            {//iOS7.0 (iOS8 以下使用)
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的登录过期，请您重新登录" delegate:self cancelButtonTitle:@"重新登录" otherButtonTitles:nil, nil];
                alert.tag = 321;
                [alert show];
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        TJJALERT_VIEW(@"网络连接错误");
    }];
}

#pragma mark -
#pragma mark 导航栏返回
- (IBAction)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 设置背景图片
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //强制竖屏
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:
                                        UIInterfaceOrientationPortrait]  forKey:@"orientation"];
    long long int currentTimeInt = [TJJDefines getTimeIntSince1970Current];
    if (currentTimeInt>=16200&& currentTimeInt <=34200) {
        self.backGround.image = [UIImage imageNamed:@"backgroundOne"];
    }else if (currentTimeInt>34200 && currentTimeInt<=55800)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundTwo"];
    }else if (currentTimeInt>55800 && currentTimeInt<=70200)
    {
        self.backGround.image = [UIImage imageNamed:@"backgroundThree"];
    }else if(currentTimeInt >70200 || currentTimeInt<16200)
    {
        self.backGround.image = [UIImage imageNamed:@"background_night"];
    }
}

#pragma mark -
#pragma mark alertView协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (alertView.tag) {
        case 321:
        {//重新登录
            UserConfig *config = [UserConfig sharingUserConfig];
            [config setHaveLogin:@"lalala"];
            [config setToken:@"123"];
            [config setUserHeadData:nil];
            
            UIStoryboard *LoginSt = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
            LoginViewController *loginVc = [LoginSt instantiateViewControllerWithIdentifier:@"loginViewId"];
            loginVc.quit = YES;
            [self.navigationController pushViewController:loginVc animated:YES];
        }
            break;
            
        default:
            break;
    }
}
@end
